package demo.practice.stack;

import java.util.Stack;

public class P232 {

    class MyQueue {
        private Stack<Integer> in;
        private Stack<Integer> out;


        /**
         * Initialize your data structure here.
         */
        public MyQueue() {
            in = new Stack<>();
            out = new Stack<>();
        }

        /**
         * Push element x to the back of queue.
         */
        public void push(int x) {
            in.add(x);
        }

        /**
         * Removes the element from in front of queue and returns that element.
         */
        public int pop() {
            if(in.size()==0&& out.size()==0)
                return -1;

            if(out.size()>0)
                return out.pop();
            else{
                //需要倒栈操作，也就是将in的所有元素都转移到out中
                while (in.size()>0)
                    out.add(in.pop());

                return out.pop();
            }
        }

        /**
         * Get the front element.
         */
        public int peek() {
            if(empty())
                return -1;
            if(out.size()==0){
                while (in.size()>0)
                    out.add(in.pop());
            }
            return out.peek();
        }

        /**
         * Returns whether the queue is empty.
         */
        public boolean empty() {
            return in.size()==0 && out.size()==0;
        }
    }
}
